package com.ruoyi.web.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.pinyin.PinYinUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.web.mapper.MesConfigMapper;
import com.ruoyi.web.mapper.MesQualityMapper;
import com.ruoyi.web.vo.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MesQualityService extends ServiceImpl<MesQualityMapper, MesQuality> {
    @Resource
    private MesConfigMapper configMapper;
    @Resource
    private MesQualityService mesQualityService;
    @Resource
    private MesQualityUserService mesQualityUserService;
    @Resource
    private MesQualityWorkService mesQualityWorkService;

    public void insertMesQuality(BaseReport baseReport, String type) {

//        // 插入主表
//        MesQuality mesQuality = new MesQuality();
//        // 生成质量处理单id
//        mesQuality.setId(UUID.randomUUID().toString().replace("-", ""));
//
//        // 生成质量处理单编号
//        Map<String, Object> map = new HashMap<>();
//        map.put("columnName", "code");
//        map.put("tableName", "mes_quality");
//        int maxNum = configMapper.getMouthMaxId(map);
//        String code = PinYinUtils.getCodeNormal(maxNum, "ZLCL");
//
//        mesQuality.setReportId(baseReport.getId());
//        mesQuality.setCode(code);
//        mesQuality.setMachineId(baseReport.getMachineId());
//        mesQuality.setMachineName(baseReport.getMachineName());
//        mesQuality.setHeadId(baseReport.getHeadId());
//        mesQuality.setHeadName(baseReport.getHeadName());
//        mesQuality.setStatus(1); // 未提交
//        mesQuality.setDisposeType(baseReport.getType());
//        mesQuality.setCreateBy(SecurityUtils.getUsername());
//        mesQuality.setCreateTime(DateUtils.getNowDate());
//        // 插入质量主表
//        mesQualityService.save(mesQuality);
//
//        List<MesQualityUser> mesQualityUserList = new ArrayList<>();
//        List<MesQualityWork> mesQualityWorklist = new ArrayList<>();
//        if (type.equals("4")) { // 覆膜
//            List<MesCoveringWork> coveringWorkList = baseReport.orderWorkList;
//            if (coveringWorkList != null && coveringWorkList.size() != 0) {
//                for (MesCoveringWork coveringWork : coveringWorkList) {
//                    List<orderb> reportWorkList = baseReport.orderWorkList;
//                    List<ReportUser> reportUserList = baseReport.orderWorkList;
//                    for (ReportUser qualitytUser : reportUserList) {
//                        MesQualityUser mesQualityUser = new MesQualityUser();
//                        mesQualityUser.setQualityId(mesQuality.getId());
//                        mesQualityUser.setNo(qualitytUser.getNo());
//                        mesQualityUser.setName(qualitytUser.getName());
//                        mesQualityUser.setCreateBy(SecurityUtils.getUsername());
//                        mesQualityUser.setCreateTime(DateUtils.getNowDate());
//                        mesQualityUserList.add(mesQualityUser);
//                    }
//                    // 批量插入罚款人员
//                    mesQualityUserService.saveBatch(mesQualityUserList);
//
//                    for (ReportWork reportWork : reportWorkList) {
//                        MesQualityWork mesQualityWork = new MesQualityWork();
//                        mesQualityWork.setQualityId(mesQuality.getId());
//                        BeanUtils.copyProperties(reportWork, mesQualityWork);
//                        mesQualityWork.setCreateBy(SecurityUtils.getUsername());
//                        mesQualityWork.setCreateTime(DateUtils.getNowDate());
//                        mesQualityWorklist.add(mesQualityWork);
//                    }
//                    // 批量插入完成工作量明细
//                    mesQualityWorkService.saveBatch(mesQualityWorklist);
//                }
//            }
//        } else {
//            List<ReportWork> reportWorkList = baseReport.reportWorkList;
//            List<ReportUser> reportUserList = baseReport.reportUserList;
//            for (ReportUser qualitytUser : reportUserList) {
//                MesQualityUser mesQualityUser = new MesQualityUser();
//                mesQualityUser.setQualityId(mesQuality.getId());
//                mesQualityUser.setNo(qualitytUser.getNo());
//                mesQualityUser.setName(qualitytUser.getName());
//                mesQualityUser.setCreateBy(SecurityUtils.getUsername());
//                mesQualityUser.setCreateTime(DateUtils.getNowDate());
//                mesQualityUserList.add(mesQualityUser);
//            }
//            // 批量插入罚款人员
//            mesQualityUserService.saveBatch(mesQualityUserList);
//
//            for (ReportWork reportWork : reportWorkList) {
//                MesQualityWork mesQualityWork = new MesQualityWork();
//                mesQualityWork.setQualityId(mesQuality.getId());
//                BeanUtils.copyProperties(reportWork, mesQualityWork);
//                mesQualityWork.setCreateBy(SecurityUtils.getUsername());
//                mesQualityWork.setCreateTime(DateUtils.getNowDate());
//                mesQualityWorklist.add(mesQualityWork);
//            }
//            // 批量插入完成工作量明细
//            mesQualityWorkService.saveBatch(mesQualityWorklist);
//
//        }


    }
}
